/********************************************/
// Creation History
// --------------------------------------------------------------
//	Company:			HTHK
//	NAME:				StaffProjectSearch.java
//    Title   :			find Staff Project Records, return one or more records
//	FUNCTION:
//	BY:				Poly Li
//	DATE:				12/01/2004
// --------------------------------------------------------------
/*********************************************/
// Copyright (c) 2002 HTHK

package tss.multiproject;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import tss.common.DBServlet2;
import tss.options.MultiStaffOptions;

import com.ffpc.sql.DataException;
import com.ffpc.struts.StrutsMessage;
import com.util.JdbcUtil;

/**
 * A Class class.
 * <P>
 * 
 * @author HTHK
 */
public class StaffProjectSearch extends DBServlet2 {
	private Logger logger = Logger.getLogger(StaffProjectSearch.class);
	@Override
	protected String perform(HttpServletRequest request,
			HttpServletResponse response, StrutsMessage message) throws DataException {

		String projectName = "";
		String staffName = "";
		String returnFlag = "";

		Map htStaffName = new TreeMap();

		if (request.getParameter("project_name") != null) {
			projectName = request.getParameter("project_name");
		}
		if (request.getParameter("staff_name") != null) {
			staffName = request.getParameter("staff_name");
		}
		if (request.getParameter("returnFlag") != null) {
			returnFlag = request.getParameter("returnFlag");
		}
		// New A Staff Project
		StaffProjectBean staffProject = StaffProjectProcess.createBean();

		// Get search condition
		staffProject.setProject_name(projectName);
		staffProject.setStaff_no(staffName);

		// Get the results
		StaffProjectBean[] staffProjects = StaffProjectProcess.find(
				this.getConnection(), staffProject);
		String staff_name[] = new String[staffProjects.length];
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			con = this.getConnection();
			stmt = con.createStatement();
			for (int m = 0; m < staffProjects.length; m++) {
				String sel_staffName = "select alias||' '||surname from tss.tss_c_employee";
				sel_staffName += " where staff_no='"
						+ staffProjects[m].getStaff_no() + "' order by alias";
				rs = stmt.executeQuery(sel_staffName);
				while (rs.next()) {
					staff_name[m] = rs.getString(1);
				}
			}
		} catch (Exception ee) {
			logger.error(ee,ee);
		} finally {
			JdbcUtil.release(stmt, rs);
		}

		// Find

		if (returnFlag.equals("y")) {
			StringBuffer sb = null;
			if (staffProjects == null || staffProjects.length == 0) {
				message.setMessage("No record, please input another criteria.");
				return "tss/multiproject/MultiProjectSearch.jsp";
			} else {
				String customOptins = "";
				Vector custom_options = new Vector();// all options
				MultiStaffOptions customOption = new MultiStaffOptions();
				custom_options = customOption.valueOptions();
				String optionFlag[] = new String[custom_options.size()];
				for (int m = 0; m < custom_options.size(); m++) {
					String customOptionsList[] = new String[1];//
					customOptionsList = (String[]) custom_options.get(m);

					for (int n = 0; n < staff_name.length; n++) {
						if (customOptionsList[0].equals(staffProjects[n]
								.getStaff_no())) {
							customOptins = getHtml(customOptionsList[0],
									staff_name[n] + " [" + customOptionsList[0]
											+ "]", true);
							optionFlag[m] = "Poly Li";
							htStaffName.put(staff_name[n], customOptins);
						} else
							continue;
					}
					if (optionFlag[m] == null) {
						String lStrStaffNname = null;
						Connection con1 = null;
						Statement stmt1 = null;
						ResultSet rs1 = null;
						try {
							con1 = this.getConnection();
							stmt1 = con1.createStatement();
							String sel_staffName = "select alias||' '||surname from tss.tss_c_employee";
							sel_staffName += " where staff_no='"
									+ customOptionsList[0] + "' order by alias";
							rs1 = stmt1.executeQuery(sel_staffName);
							rs1.next();
							lStrStaffNname = rs1.getString(1);

						} catch (Exception ee) {
							logger.error(ee,ee);
						} finally {
							JdbcUtil.release(stmt1, rs1);
						}
						customOptins = getHtml(customOptionsList[0],
								lStrStaffNname + " [" + customOptionsList[0]
										+ "]", false);
						htStaffName.put(lStrStaffNname, customOptins);
					}
				}
				// getOptions(htStaffName);
				// Vector mm = (Vector)htStaffName;
				// logger.debug(" mm length is : "+mm.size());
				// logger.debug("map is : "+htStaffName.values());
				request.setAttribute("staffProjects", staffProjects);
				request.setAttribute("staff_name", staff_name);
				// request.setAttribute("customOptins",customOptins);
				request.setAttribute("customOptins", getOptions(htStaffName));
				return "tss/multiproject/MultiProject.jsp";
			}
		} else {
			if (staffProjects == null || staffProjects.length == 0) {
				message.setMessage("No record, please input another criteria.");
				return "tss/multiproject/MultiProjectSearch.jsp";
			}
			// find more than one records
			else {
				request.setAttribute("staffProjects", staffProjects);
				request.setAttribute("staff_name", staff_name);
				return "tss/multiproject/MultiProjectList.jsp";
			}
		}
	}

	public String getHtml(String value, String description, boolean selected) {
		StringBuffer sb = new StringBuffer("   <option value=\"").append(value)
				.append("\"").append(selected ? " selected " : " ")
				.append(" >").append(description).append("</option>\n");
		return sb.toString();
	}

	public String getOptions(Map map) {
		Collection entrySet = map.entrySet();
		Iterator it = entrySet.iterator();
		StringBuffer sb = new StringBuffer();
		while (it.hasNext()) {
			Map.Entry e = (Map.Entry) it.next();
			// logger.debug("Key = "+e.getKey()+", Value is : "+e.getValue());
			sb.append(e.getValue());
		}
		return sb.toString();
	}
}
